package com.handinfo.engine;

import javax.microedition.lcdui.*;

//#if lucency == "YES"
//import com.nokia.mid.ui.DirectGraphics;
//import com.nokia.mid.ui.DirectUtils;
//#endif
//#if screen == "640360"
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;
//#endif
/**
 * 游戏画布基类
 * 
 * @author Xia Mingwei
 * @version 1.0
 */
public abstract class AbstractGameScreen extends Canvas
{
	public static AbstractGameScreen s_instance;

	//#if screen == "320240"
	//# public final static int SCREEN_WIDTH = 320; // 屏幕宽度
	//# public final static int SCREEN_HEIGHT = 240; // 屏幕高度
	//#elif screen == "640360"
	 public final static int SCREEN_WIDTH = 480; // 屏幕宽度
	 public final static int SCREEN_HEIGHT = 320; // 屏幕高度
	//#else
//	public final static int SCREEN_WIDTH = 240; // 屏幕宽度
//	public final static int SCREEN_HEIGHT = 320; // 屏幕高度
//	public final static int SCREEN_WIDTH = 176; // 屏幕宽度
//	public final static int SCREEN_HEIGHT = 208; // 屏幕高度
	//#endif
	public final static int SCREEN_OFFSET_X = 0; // 屏幕偏移
	public final static int SCREEN_OFFSET_Y = 0; // 屏幕偏移
	public static int FONT_H; // 字体高度
	public static final Font s_font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	public static final Font s_font_med = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
	public static final Font s_font_big = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
	public static int FONT_W = s_font.stringWidth("一");
	private Image m_bufferImage; // 全局缓存图像
	private Graphics m_g;
	//#if lucency == "YES"
//	private DirectGraphics m_dg;
    //#endif

	/**
	 * 游戏画布构造函数
	 */
	public AbstractGameScreen()
	{
		setFullScreenMode(true);
		FONT_H = s_font.getHeight();
		m_bufferImage = Image.createImage(SCREEN_WIDTH, SCREEN_HEIGHT); // 创建缓存图
		m_g = m_bufferImage.getGraphics(); // 获得
		m_g.setFont(s_font);
		//#if lucency == "YES"
//		m_dg = DirectUtils.getDirectGraphics(m_g);
		//#endif
		s_instance = this;
//		//#if screen = "640360"
//		 initTouchImage();
//		//#endif
		

	}

	public Image getBufImage()
	{
		return m_bufferImage;
	}

	Animation animTest;
	// public static final int FRAME_INTERVAL = 80; //每帧间隔
	public static final int GS_TEST = -1;
	public static final int GS_LOGIN = 0;// 登录状态
	public static final int GS_PERSON = 1;// 人物界面状态
	public static final int GS_PLAYING = 2; // 游戏中状态
	protected int m_gameStatus = GS_LOGIN; // 游戏状态值，默认进入登录界面
//	private static boolean s_isRunning = true; // 游戏世界是否运行
//	private static int s_frameCount; // 帧计数器
	public static int s_tip_id = 0; // 进度条提示ID
	public int m_shakeFrame = 0; // 屏幕震动持续帧数
	public int m_shakeX = 0; // 屏幕震动时绘制坐标X
	public int m_shakeY = 0; // 屏幕震动时绘制坐标Y
	public int m_flashFrame = 0; // 屏幕闪屏持续帧数
	public int[] m_flashColor; // 屏幕闪屏的颜色
	public static int s_level = 0;


	/**
	 * 切换游戏状态，目前仅分为主菜单状态和场景执行状态
	 * 
	 * @param newStatus
	 * 切换的状态
	 */
	public void setGameStatus(int newStatus)
	{
//		s_frameCount = 0;
		m_gameStatus = newStatus;
	}

	/**
	 * 获得图形上下文
	 * 
	 * @return
	 */
	public Graphics getGraphics()
	{
		return m_g;
	}

	//#if lucency == "YES"
//	public DirectGraphics getDirectGraphics()
//	{
//		return m_dg;
//	}
    //#endif

	/**
	 * paint方法只做一件事情，将缓存图绘制到屏幕上
	 */
	public void paint(Graphics g)
	{  
//		//#if screen == "640360"
//		 drawTouchImage(g);
//		 g.translate(140, 0);
//		//#endif
		g.drawImage(m_bufferImage, 0 + m_shakeX, 0 + m_shakeY, 0);
		flash(g);
		shake();
	}

	public void logic()
	{
	}

	public void setShake(int shakeFrame, int x_offset, int y_offset)
	{
		m_shakeFrame = shakeFrame;
		m_shakeX = x_offset;
		m_shakeY = y_offset;
	}

	/**
	 * 震屏
	 */
	public void shake()
	{
		if (m_shakeFrame > 0) {
			m_shakeFrame--;
			if (m_shakeFrame % 2 == 1) {
				m_shakeY = -m_shakeY;
				m_shakeX = -m_shakeX;
				// m_shakeY = 4;
			}
			else {
				// m_shakeY = -4;
			}
		}
		else {
			m_shakeY = 0;
			m_shakeX = 0;
		}
	}

	public void setFlash(int frame, int[] color)
	{
		if (m_flashFrame <= 0) {
			m_flashFrame = frame;
			m_flashColor = color;
		}
	}

	/**
	 * 闪屏
	 */
	public void flash(Graphics g)
	{
		if (m_flashFrame > 0) {
			m_flashFrame--;
			if (m_flashColor != null && m_flashFrame < m_flashColor.length
					&& m_flashColor[m_flashFrame] != -1) {
				g.setColor(m_flashColor[m_flashFrame]);
				g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH,
						AbstractGameScreen.SCREEN_HEIGHT);
				g.fillRect(0, 0, AbstractGameScreen.SCREEN_WIDTH,
						AbstractGameScreen.SCREEN_HEIGHT);
			}
		}
		else {
			m_flashFrame = 0;
			m_flashColor = null;
		}
	}

	/**
	 * 剪裁区变量
	 */
	public static int s_clipX;
	public static int s_clipY;
	public static int s_clipW;
	public static int s_clipH;

	/**
	 * 设置剪裁区
	 * 
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public static void setClip(Graphics g, int x, int y, int w, int h)
	{
		s_clipX = g.getClipX();
		s_clipY = g.getClipY();
		s_clipW = g.getClipWidth();
		s_clipH = g.getClipHeight();
		g.setClip(x, y, w, h);
		g.clipRect(x, y, w, h);
	}

	/**
	 * 恢复剪裁区
	 * 
	 * @param g
	 */
	public static void restoreClip(Graphics g)
	{
		g.setClip(s_clipX, s_clipY, s_clipW, s_clipH);
		g.clipRect(s_clipX, s_clipY, s_clipW, s_clipH);
	}

	/**
	 * 获得翻转状态的标记值
	 * 
	 * @param flipX
	 * 是否水平翻转
	 * @param flipY
	 * 是否垂直翻转
	 * @return
	 */
	public static int trans(boolean flipX, boolean flipY)
	{
		
		if (flipX) {
			if (flipY) {
				return javax.microedition.lcdui.game.Sprite.TRANS_ROT180;
			}
			else {
				return javax.microedition.lcdui.game.Sprite.TRANS_MIRROR;
			}
		}
		else {
			if (flipY) {
				return javax.microedition.lcdui.game.Sprite.TRANS_MIRROR_ROT180;
			}
			else {
				return javax.microedition.lcdui.game.Sprite.TRANS_NONE;
			}
		}
	}

	/**
	 * 获得翻转及旋转状态的标记值
	 * 
	 * @param flag
	 * int 工具导出的标记
	 * @return int
	 */
	public static int trans(int flag)
	{
		switch (flag) {
			case 1:
				return javax.microedition.lcdui.game.Sprite.TRANS_MIRROR;
			case 2:
				return javax.microedition.lcdui.game.Sprite.TRANS_MIRROR_ROT180;
			case 3:
				return javax.microedition.lcdui.game.Sprite.TRANS_ROT180;
			case 4:
				return javax.microedition.lcdui.game.Sprite.TRANS_ROT90;
			case 5: // Mirror后旋转方向跟Sprite的相反 所以工具中的MIRROR_ROT90 ==
				// Sprite中的TRANS_MIRROR_ROT270
				return javax.microedition.lcdui.game.Sprite.TRANS_MIRROR_ROT270;
			case 6: // Mirror后旋转方向跟Sprite的相反 所以工具中的MIRROR_ROT270 ==
				// Sprite中的TRANS_MIRROR_ROT90
				return javax.microedition.lcdui.game.Sprite.TRANS_MIRROR_ROT90;
			case 7:
				return javax.microedition.lcdui.game.Sprite.TRANS_ROT270;
			default:
				return javax.microedition.lcdui.game.Sprite.TRANS_NONE;
		}
	}

	/**
	 * 统一drawRegion方法，在所有需要使用drawRegion的地方都调用这个方法 该方法整合MIDP2.0和Nokia的绘制
	 * 
	 * @param g
	 * Graphics
	 * @param img
	 * Image
	 * @param x_src
	 * int
	 * @param y_src
	 * int
	 * @param width
	 * int
	 * @param height
	 * int
	 * @param transform
	 * int
	 * @param x_dest
	 * int
	 * @param y_dest
	 * int
	 * @param anchor
	 * int
	 */
	public static void drawRegion(Graphics g, Image img, int x_src, int y_src,
			int width, int height, int transform, int x_dest, int y_dest,
			int anchor)
	{
		if (x_src < 0 || y_src < 0) {
			return;
		}
		// 消除绘制越界
		if (x_src + width > img.getWidth()) {
			width = img.getWidth() - x_src;
		}
		if (y_src + height > img.getHeight()) {
			height = img.getHeight() - y_src;
		}
		g.drawRegion(img, x_src, y_src, width, height, transform, x_dest,
				y_dest, anchor);
	}
	

	public static void drawString(Graphics g, String str, int x, int y,
			int anchor)
	{
		switch (anchor) {
			case Graphics.VCENTER | Graphics.HCENTER:
				g.drawString(str, x, y - FONT_H / 2, Graphics.TOP
						| Graphics.HCENTER);
				break;
			case Graphics.LEFT | Graphics.VCENTER:
				g.drawString(str, x, y, Graphics.TOP | Graphics.LEFT);
				break;
			default:
				g.drawString(str, x, y, anchor);
				break;
		}
	}

	// 图像处理工具方法代码块结束
	// ///////////////////////////////////////////////////////////
	// 会在屏幕隐藏前被调用
	protected void hideNotify()
	{
	}

	// 会在屏幕显示前被调用
	protected void showNotify()
	{
		keyCurrent = 0;
		keyPressed = 0;
		keyOff = 0;
		key = 0;
	}

	// ///////////////////////////////////////////////////////////
	// 按键处理代块开始……
	public static final int GK_UP = 1;
	// public static final int GK_UPRIGHT = 1 << 1;
	public static final int GK_RIGHT = 1 << 1;
	// public static final int GK_DOWNRIGHT = 1 << 3;
	public static final int GK_DOWN = 1 << 2;
	// public static final int GK_DOWNLEFT = 1 << 5;
	public static final int GK_LEFT = 1 << 3;
	// public static final int GK_UPLEFT = 1 << 7;
	public static final int GK_KEY1 = 1 << 4;
	public static final int GK_KEY2 = 1 << 5;
	public static final int GK_KEY3 = 1 << 6;
	public static final int GK_KEY4 = 1 << 7;
	public static final int GK_KEY5 = 1 << 8;
	public static final int GK_KEY6 = 1 << 9;
	public static final int GK_KEY7 = 1 << 10;
	public static final int GK_KEY8 = 1 << 11;
	public static final int GK_KEY9 = 1 << 12;
	public static final int GK_A = 1 << 13;
	public static final int GK_B = 1 << 14; // “#”
	public static final int GK_KEY0 = 1 << 15; // “0”
	public static final int GK_D = 1 << 16; // “*”
	public static final int GK_LEFT_SOFT = 1 << 17;
	public static final int GK_RIGHT_SOFT = 1 << 18;
	public static final int GK_GAME_A = 1 << 19;
	public static final int GK_GAME_B = 1 << 20;
	public static final int GK_GAME_C = 1 << 21;
	public static final int GK_GAME_D = 1 << 22;
	private static final int GAME_KEY_COUNT = 23;
	private static int keyCurrent = 0;

	static final int getKeyCurrent()
	{
		return keyCurrent;
	}

	private static int keyPressed = 0;
	private static int key = 0;
	private static int keyOff = 0;
	// 按键buffer部分
	private static final int KEYBUFFER_MAX = 5;
	private static final int KEYPRESSED_INTEVAL_MAX = 4;
	private static int[] keyBuffer = new int[KEYBUFFER_MAX]; // 按键序列
	private static int keyBufferCursor = 0;
	private static int keyPressedInterval = 0;
	private static byte[] keyHoldFrame = new byte[GAME_KEY_COUNT]; // 按键hold的帧数
	private static boolean needKeyBuffer = false;

	public static void setNeedKeyBuffer(boolean _needKeyBuffer)
	{
		needKeyBuffer = _needKeyBuffer;
	}

	public static final void addKeyBuffer(int gameKey)
	{
		synchronized (keyBuffer) {
			if (keyBufferCursor >= KEYBUFFER_MAX) {
				execKeyBuffer();
			}
			keyBuffer[keyBufferCursor++] |= gameKey;
		}
	}

	public static final void execKeyBuffer()
	{
		synchronized (keyBuffer) {
			for (int i = 0; i < keyBufferCursor - 1; i++) {
				keyBuffer[i] = keyBuffer[i + 1];
			}
			keyBuffer[keyBufferCursor - 1] = 0;
			keyBufferCursor--;
		}
	}

	public static final void cleanKeyBuffer()
	{
		synchronized (keyBuffer) {
			for (int i = 0; i < keyBufferCursor; i++) {
				keyBuffer[i] = 0;
			}
			keyBufferCursor = 0;
		}
	}

	public static final void updateKeyHoldFrame()
	{
		synchronized (keyHoldFrame) {
			for (int i = 0; i < GAME_KEY_COUNT; i++) {
				if ((keyCurrent & (1 << i)) != 0) {
					if (keyHoldFrame[i] < 127) {
						keyHoldFrame[i]++;
					}
				}
				else {
					keyHoldFrame[i] = 0;
				}
			}
		}
	}

	public static final void cleanKeyHoldFrame()
	{
		synchronized (keyHoldFrame) {
			for (int i = 0; i < GAME_KEY_COUNT; i++) {
				keyHoldFrame[i] = 0;
			}
		}
	}

	public static final boolean haveKeyDoubleClicked(int gamekey)
	{
		if (!needKeyBuffer || keyBufferCursor < 2) {
			return false;
		}
		if (keyBuffer[keyBufferCursor - 1] == gamekey
				&& keyBuffer[keyBufferCursor - 2] == gamekey) {
			cleanKeyBuffer();
			return true;
		}
		return false;
	}

	public static final boolean haveKeyHold(int gamekey, int holdframe)
	{
		return needKeyBuffer && (keyCurrent & gamekey) != 0
				&& keyHoldFrame[getKeyInx(gamekey)] >= holdframe;
	}

	// 按键buffer部分
	//#if key == "MOTO_SPECIAL"
	//# private static final int UP_ARROW = -1;
	//# private static final int DOWN_ARROW = -2;
	//# private static final int LEFT_ARROW = -3;
	//# private static final int RIGHT_ARROW = -4;
	//# private static final int MIDDLE = -5;
	//# public static final int LEFT_SOFT = -21;
	//# public static final int RIGHT_SOFT = -22;
	//#elif key == "MOTO_DEFAULT"
	//# private static final int UP_ARROW = 1;
	//# private static final int DOWN_ARROW = 6;
	//# private static final int LEFT_ARROW = 2;
	//# private static final int RIGHT_ARROW = 5;
	//# private static final int MIDDLE = 20;
	//# public static final int LEFT_SOFT = 21;
	//# public static final int RIGHT_SOFT = 22;
	//#elif key == "MOTO_E"
	//# private static final int UP_ARROW = 1;
	//# private static final int DOWN_ARROW = 2;
	//# private static final int LEFT_ARROW = 3;
	//# private static final int RIGHT_ARROW = 4;
	//# private static final int MIDDLE = 5;
	//# public static final int LEFT_SOFT = 21;
	//# public static final int RIGHT_SOFT = 22;
	//#else
	public static final int UP_ARROW = -1;
 	public static final int DOWN_ARROW = -2;
	public static final int LEFT_ARROW = -3;
 	public static final int RIGHT_ARROW = -4;
	public static final int MIDDLE = -5;
 	public static final int LEFT_SOFT = -6;
	public static final int RIGHT_SOFT = -7;
	//#endif
	
	//#if screen == "320240"
	//# private static final int getKey(int keyCode)
	//# {
	//#if key == "NOKIA_DEFAULT"
	//# switch (keyCode) {
	//#	case UP_ARROW:
			// case KEY_NUM2:
	//#		return GK_UP;

	//#	case DOWN_ARROW:
	//#		return GK_DOWN;

	//#	case LEFT_ARROW:
	//#		return GK_LEFT;

	//#	case RIGHT_ARROW:
	//#		return GK_RIGHT;

	//#	case KEY_NUM1:
	//#		return GK_KEY1;

	//#	case KEY_NUM2:
	//#		return GK_KEY2;

	//#	case KEY_NUM3:
	//#		return GK_KEY3;

	//#	case KEY_NUM4:
	//#		return GK_KEY4;

	//#	case KEY_NUM5:
	//#		return GK_KEY5;

	//#	case KEY_NUM6:
	//#		return GK_KEY6;

	//#	case KEY_NUM7:
	//#		return GK_KEY7;

	//#	case KEY_NUM8:
	//#		return GK_KEY8;

	//#	case KEY_NUM9:
	//#		return GK_KEY9;

	//#	case MIDDLE:
	//#		return GK_A;

	//#	case KEY_POUND:
	//#		return GK_B;

	//#	case KEY_NUM0:
	//#		return GK_KEY0;

	//#	case KEY_STAR:
	//#		return GK_D;

	//#	case LEFT_SOFT:
	//#		return GK_LEFT_SOFT;

	//# case RIGHT_SOFT:
	//#	return GK_RIGHT_SOFT;

	//#	default:
	//#		return 0;
	//#		}
	//#else
	//#	switch (keyCode) {
	//#		case UP_ARROW:
	//#			return GK_UP;

	//#		case DOWN_ARROW:
	//#			return GK_DOWN;

	//#		case LEFT_ARROW:
	//#			return GK_LEFT;

	//#		case RIGHT_ARROW:
	//#			return GK_RIGHT;
				
	//#		case 109:
	//#			return GK_KEY0;

	//#		case 114:
	//#			return GK_KEY1;

	//#		case 116:
	//#			return GK_KEY2;

	//#		case 121:
	//#			return GK_KEY3;

	//#		case 102:
	//#			return GK_KEY4;

	//#		case 103:
	//#			return GK_KEY5;

	//#		case 104:
	//#			return GK_KEY6;

	//#		case 118:
	//#			return GK_KEY7;

	//#		case 98:
	//#			return GK_KEY8;

	//#		case 110:
	//#			return GK_KEY9;

	//#		case MIDDLE:
	//#			return GK_A;

	//#		case 106://#
	//#			return GK_B;

	//#		case 117://*
	//#			return GK_D;

	//#		case LEFT_SOFT:
	//#			return GK_LEFT_SOFT;

	//#		case RIGHT_SOFT:
	//#			return GK_RIGHT_SOFT;

	//#		default:
	//#			return 0;
	//#	}
	//#endif

	//# }
	//#else
	private static final int getKey(int keyCode)
	{
		//#if key == "MOTO_E" || key == "MOTO_DEFAULT"
		//# if (keyCode < 0) {
		//# 	keyCode = -keyCode;
		//# }
		//#endif
		
		switch (keyCode) {
			case UP_ARROW:
				// case KEY_NUM2:
				return GK_UP;

			case DOWN_ARROW:
				return GK_DOWN;

			case LEFT_ARROW:
				return GK_LEFT;

			case RIGHT_ARROW:
				return GK_RIGHT;

			case KEY_NUM1:
				return GK_KEY1;

			case KEY_NUM2:
				return GK_KEY2;

			case KEY_NUM3:
				return GK_KEY3;

			case KEY_NUM4:
				return GK_KEY4;

			case KEY_NUM5:
				return GK_KEY5;

			case KEY_NUM6:
				return GK_KEY6;

			case KEY_NUM7:
				return GK_KEY7;

			case KEY_NUM8:
				return GK_KEY8;

			case KEY_NUM9:
				return GK_KEY9;

			case MIDDLE:
				return GK_A;

			case KEY_POUND:
				return GK_B;

			case KEY_NUM0:
				return GK_KEY0;

			case KEY_STAR:
				return GK_D;

			case LEFT_SOFT:
				return GK_LEFT_SOFT;

			case RIGHT_SOFT:
				return GK_RIGHT_SOFT;

			default:
				return 0;
		}
	}
    //#endif
	
	public static boolean s_rightConfirm = false;

	public static final void setKeyCustom(boolean confirmType)
	{
		s_rightConfirm = confirmType;
	}

	public static final int getKeyInx(int gamekey)
	{
		for (int i = 0; i < GAME_KEY_COUNT; i++) {
			if ((gamekey & (1 << i)) != 0) {
				return i;
			}
		}
		return -1;
	}

	public static final void updateKeyBegin()
	{
		keyPressed = ~key & keyCurrent;
		key = keyCurrent;

		if (needKeyBuffer) {
			if (keyCurrent != 0) { // 有pressed或者hold，更新按键hold帧数
				updateKeyHoldFrame();
			}

			if (keyPressed == 0) { // 没有press事件，累加interval，超过一定时间清空keyBuffer
				if (++keyPressedInterval > KEYPRESSED_INTEVAL_MAX) {
					cleanKeyBuffer();
				}
			}
			else {
				keyPressedInterval = 0;
			}
		}
	}

	public static final void updateKeyEnd()
	{
		keyCurrent &= ~keyOff;
		keyOff = 0;
		if (pointOff) {
			pointX = -1;
			pointY = -1;
		}
	}

	public final void keyPressed(int keyCode)
	{
		int gameKey = getKey(keyCode);
		keyCurrent |= gameKey;
	}

	public final void keyReleased(int keyCode)
	{
		int gameKey = getKey(keyCode);
		if ((key & gameKey) != 0) {
			keyCurrent &= ~gameKey;
		}
		else {
			keyOff |= gameKey;
		}

		if (needKeyBuffer) {
			addKeyBuffer(gameKey);
			cleanKeyHoldFrame();
		}
	}

	public static final boolean haveKeyHold(int gameKey)
	{
		return (keyCurrent & gameKey) != 0;
	}

	public static final boolean haveKeyPressed(int gameKey)
	{
		return (keyPressed & gameKey) != 0;
	}

	public static final boolean anyKeyHold()
	{
		return keyCurrent != 0;
	}

	public static final boolean anyKeyPressed()
	{
		return haveKeyPressed(GK_LEFT_SOFT | GK_RIGHT_SOFT | GK_LEFT | GK_RIGHT
				| GK_UP | GK_DOWN | GK_KEY1 | GK_KEY2 | GK_KEY3 | GK_KEY4
				| GK_KEY5 | GK_KEY6 | GK_KEY7 | GK_KEY8 | GK_KEY9 | GK_A | GK_B
				| GK_KEY0 | GK_D);
	}

	public static final boolean runKeyPressed()
	{
		boolean runkey = haveKeyPressed(GK_LEFT | GK_RIGHT);
		runkey = runkey || haveKeyHold(GK_LEFT | GK_RIGHT);
		return runkey;
	}

	public static final boolean runLeftKeyPressed()
	{
		boolean runkey = haveKeyPressed(GK_LEFT);
		runkey = runkey || haveKeyHold(GK_LEFT);
		return runkey;
	}

	public static final boolean runRightKeyPressed()
	{
		boolean runkey = haveKeyPressed(GK_RIGHT);
		runkey = runkey || haveKeyHold(GK_RIGHT);
		return runkey;
	}

	public static final boolean keyCanceled()
	{
		if (s_rightConfirm) {
			return haveKeyPressed(GK_LEFT_SOFT);
		}
		else {
			return haveKeyPressed(GK_RIGHT_SOFT);
		}
	}

	public static final boolean keyConfirmed()
	{
		int confirmKey = 0;
		if (s_rightConfirm) {
			confirmKey = GK_RIGHT_SOFT | GK_A;
		}
		else {
			confirmKey = GK_LEFT_SOFT | GK_A;
		}
		return haveKeyPressed(confirmKey);
	}
	
	 public static boolean pointOff = true; //无触摸
	 public static int pointX = -1, pointY = -1; //当前触摸点
	 
	 public static int getPointX()
	 {
		return pointX;
	 }

	 public static int getPointY()
	 {
		return pointY;
	 }
	 

	protected void pointerReleased(int x, int y) {
		showNotify();
	}
	

	protected void pointerPressed(int x, int y) {
		GameTouchManager.checkPressed(x, y);
	}

	 
	 
//	//#if screen == "640360"
//	private static Image touch_left;
//	private static Image touch_right;
//	private static Image touch_sure;
//	private static Image touch_circle;
//	private static Image touch_big_button;
//	private static Image touch_button;
//	private static Image touch_number;
//	 private static Image touch_arrow;
//
//	  private static void initTouchImage()
//	 {
//		try {
//			touch_left = Image.createImage("/img/touchkey/zuoruanjian.png");
//			touch_right = Image.createImage("/img/touchkey/youruanjian.png");
//			touch_sure = Image.createImage("/img/touchkey/queding1.png");
//			touch_circle = Image.createImage("/img/touchkey/daanniu.png");
//			touch_big_button = Image.createImage("/img/touchkey/zouyouanniu.png");
//			touch_button = Image.createImage("/img/touchkey/shuzianniu.png");
//			touch_number = Image.createImage("/img/touchkey/anniushuzi.png");
//			touch_arrow = Image.createImage("/img/touchkey/jiantou.png");
//		}
//		catch (Exception e) {
//			Tools.debug(e);
//		}
//	 }
//	
//	 private void drawTouchImage(Graphics g)
//	 {
//	 //绘制黑屏
//		g.setColor(0x0);
//		g.fillRect(0, 0, 640, 360);
//     //绘制左边按钮
//		int w = touch_circle.getWidth();
//		int h = touch_circle.getHeight();
//		for (int i = 0; i < 4; i++) {
//			Tools.drawImage(g, touch_circle, TOUCH_AREAS[i][0], TOUCH_AREAS[i][1], w, h, 0);
//		}
//	 //绘制右边确认按钮
//	 Tools.drawImage(g, touch_circle, TOUCH_AREAS[13][0], TOUCH_AREAS[13][1], w, h, 0);
//	 int w1 = touch_sure.getWidth();
//	 int h1 = touch_sure.getHeight();
//	 Tools.drawImage(g, touch_sure, (TOUCH_AREAS[13][0]+(w>>1)), (TOUCH_AREAS[13][1]+(h>>1)), w, h, Graphics.HCENTER | Graphics.VCENTER);
//	 //绘制方向箭头
//	 w1 = touch_arrow.getWidth();
//	 h1 = touch_arrow.getHeight();
//	 Tools.drawRegion(g, touch_arrow, (TOUCH_AREAS[0][0]+(w>>1)), (TOUCH_AREAS[0][1]+(h>>1)), 0, Graphics.HCENTER | Graphics.VCENTER);
//	 Tools.drawRegion(g, touch_arrow, (TOUCH_AREAS[1][0]+(w>>1)), (TOUCH_AREAS[1][1]+(h>>1)), 5, Graphics.HCENTER | Graphics.VCENTER);
//	 Tools.drawRegion(g, touch_arrow, (TOUCH_AREAS[2][0]+(w>>1)), (TOUCH_AREAS[2][1]+(h>>1)), 1, Graphics.HCENTER | Graphics.VCENTER);
//	 Tools.drawRegion(g, touch_arrow, (TOUCH_AREAS[3][0]+(w>>1)), (TOUCH_AREAS[3][1]+(h>>1)), 4, Graphics.HCENTER | Graphics.VCENTER);
//	 //绘制左右软键
//	 w = touch_big_button.getWidth();
//	 h = touch_big_button.getHeight();
//	 w1 = touch_left.getWidth();
//	 h1 = touch_left.getHeight();
//	 Tools.drawImage(g,touch_big_button,TOUCH_AREAS[17][0], TOUCH_AREAS[17][1],w,h,0);
//	 Tools.drawImage(g,touch_left,(TOUCH_AREAS[17][0]+(w>>1)),(TOUCH_AREAS[17][1]+(h>>1)),w1,h1,Graphics.HCENTER | Graphics.VCENTER);
//	 Tools.drawImage(g,touch_big_button,TOUCH_AREAS[18][0], TOUCH_AREAS[18][1],w,h,0);
//	 w1 = touch_right.getWidth();
//	 h1 = touch_right.getHeight();
//	 Tools.drawImage(g,touch_right,(TOUCH_AREAS[18][0]+(w>>1)),(TOUCH_AREAS[18][1]+(h>>1)),w1,h1,Graphics.HCENTER | Graphics.VCENTER);
//	 //绘制右边数字按钮
//	 w = touch_button.getWidth();
//	 h = touch_button.getHeight();
//	 w1 = touch_number.getWidth()/12;
//	 h1 = touch_number.getHeight();
//	 for( int i=4;i<13;i++ ){
//	    Tools.drawImage(g, touch_button, TOUCH_AREAS[i][0], TOUCH_AREAS[i][1], w, h, 0);
//	    Tools.drawNumber(g, touch_number, (TOUCH_AREAS[i][0]+(w>>1)), (TOUCH_AREAS[i][1]+(h>>1)), w1, h1,
//	             (i-4), 0, Graphics.HCENTER | Graphics.VCENTER, false);
//	 }
//	    Tools.drawImage(g, touch_button, TOUCH_AREAS[14][0], TOUCH_AREAS[14][1], w, h, 0);
//	    g.drawRegion(touch_number, w1*11, 0, w1, h1, 0, (TOUCH_AREAS[14][0]+(w>>1)), (TOUCH_AREAS[14][1]+(h>>1)),
//	                 Graphics.HCENTER | Graphics.VCENTER);
//	    Tools.drawImage(g, touch_button, TOUCH_AREAS[15][0], TOUCH_AREAS[15][1], w, h, 0);
//	    g.drawRegion(touch_number, w1*9, 0, w1, h1, 0, (TOUCH_AREAS[15][0]+(w>>1)), (TOUCH_AREAS[15][1]+(h>>1)),
//	                 Graphics.HCENTER | Graphics.VCENTER);
//	    Tools.drawImage(g, touch_button, TOUCH_AREAS[16][0], TOUCH_AREAS[16][1], w, h, 0);
//	    g.drawRegion(touch_number, w1*10, 0, w1, h1, 0, (TOUCH_AREAS[16][0]+(w>>1)), (TOUCH_AREAS[16][1]+(h>>1)),
//	                 Graphics.HCENTER | Graphics.VCENTER);
//	 }
//	 public static final int circle_width = 68;
//	 public static final int circle_height = 68;
//	 public static final int circle_up_x = 36;
//	 public static final int circle_up_y = 38;
//	 public static final int circle_down_x = 36;
//	 public static final int circle_down_y = 164;
//	 public static final int circle_left_x = 2;
//	 public static final int circle_left_y = 102;
//	 public static final int button_width = 46;
//	 public static final int button_height = 36;
//	 public static final int button_w = 1;//button 列间隙
//	 public static final int button_h = 10;//button 行间隙
//	 public static final int button_x = 500;
//	 public static final int button_x1 = (button_x+button_width+button_w);
//	 public static final int button_x2 = (button_x+button_width+button_w+button_width+button_w);
//	 public static final int button_y = 36;
//	 public static final int button_y1 = (button_y+button_height+button_h);
//	 public static final int button_y2 = (button_y+button_height+button_h+button_height+button_h);
//	 public static final int button_y3 = (button_y+button_height+button_h+button_height+button_h+button_height+button_h);
//	 public static final int sure_x = 536;
//	 public static final int sure_y = 232;
//	 public static final int bigbutton_width = 102;
//	 public static final int bigbutton_height = 49;
//	 public static final int bigbutton_h = 2;
//     public static final int left_x = 19;
//	 public static final int right_x = 500+19;
//	 public static final int soft_y = 360 - bigbutton_h - bigbutton_height;
//	 public static final int[][] TOUCH_AREAS = {
//		{circle_up_x,circle_up_y,(circle_up_x+circle_width),(circle_up_y+circle_height)}, //up
//		{(circle_left_x+circle_width),circle_left_y,(circle_left_x+circle_width+circle_width),(circle_left_y+circle_height)}, //right
//		{circle_down_x,circle_down_y,(circle_down_x+circle_width),(circle_down_y+circle_height)}, //down
//		{circle_left_x,circle_left_y,(circle_left_x+circle_width),(circle_left_y+circle_height)}, //left
//	
//	 {button_x,button_y,(button_x+button_width),(button_y+button_height)}, //1
//		{button_x1,button_y,(button_x1+button_width),(button_y+button_height)}, //2
//		{button_x2,button_y,(button_x2+button_width),(button_y+button_height)}, //3
//	 
//		{button_x,button_y1,(button_x+button_width),(button_y1+button_height)}, //4
//		{button_x1,button_y1,(button_x1+button_width),(button_y1+button_height)}, //5
//		{button_x2,button_y1,(button_x2+button_width),(button_y1+button_height)}, //6
//	
//		{button_x,button_y2,(button_x+button_width),(button_y2+button_height)}, //7
//		{button_x1,button_y2,(button_x1+button_width),(button_y2+button_height)}, //8
//		{button_x2,button_y2,(button_x2+button_width),(button_y2+button_height)}, //9
//	
//		{sure_x,sure_y,(sure_x+circle_width),(sure_y+circle_height)}, //A "确定" 13
//	
//		{button_x2,button_y3,(button_x2+button_width),(button_y3+button_height)}, //B "#"
//		{button_x1,button_y3,(button_x1+button_width),(button_y3+button_height)}, //0
//		{button_x,button_y3,(button_x+button_width),(button_y3+button_height)}, //D "*"
//	
//		{left_x,soft_y,(left_x+bigbutton_width),(soft_y+bigbutton_height)}, //left-soft
//		{right_x,soft_y,(right_x+bigbutton_width),(soft_y+bigbutton_height)}, //right-soft
//	 };
//	 int m_gameKey = 0;
//	 protected final void pointerPressed(int x, int y)
//	 {
//		//int gameKey = getTouchKey(x, y);
//	 m_gameKey = getTouchKey(x, y);
//		if ( m_gameKey != 0 ) {
//			touchKeyPressed(m_gameKey);
//		}
//	 }
//
//	 protected final void pointerReleased(int x, int y)
//	 {
//		//int gameKey = getTouchKey(x, y);
//		if (m_gameKey != 0) {
//			touchKeyReleased(m_gameKey);
//		}
//		else {
//			pointOff = true;
//		}
//	 }
//	
//	 protected final void touchKeyPressed(int gameKey)
//	 {
//		keyCurrent |= gameKey;
//	 }
//
//	 protected final void touchKeyReleased(int gameKey)
//	 {
//		if ((key & gameKey) != 0) {
//			keyCurrent &= ~gameKey;
//		}
//		else {
//			keyOff |= gameKey;
//		}
//
//		if (needKeyBuffer) {
//			addKeyBuffer(gameKey);
//			cleanKeyHoldFrame();
//		}
//	 }
//	
//	 public int getTouchKey(int x, int y)
//	 {
//		for (int i = 0; i < TOUCH_AREAS.length; i++) {
//			if (Tools.pointInRect(x, y, TOUCH_AREAS[i])) {
//				return 1 << i;
//				}
//		}
//		return 0;
//	 }
//    //#endif
	

	// 按键处理代码块结束
	// ///////////////////////////////////////////////////////////
	// ///////////////////////////////////////////////////////////
	// 资源文件定义开始
	public static final String FILE_EVENTENGINE = "/EVT"; // 变量
	public static final String FILE_ANIMATION = "/ANIM";
	public static final String FILE_BACKGROUND = "/BG";
	public static final String FILE_SCENE = "/SCN";
	public static final String FILE_MUSIC = "/m";
	public static final String FILE_DATA = "/dat";

	public abstract void requestResource(byte[] type, String[] url);

	// 资源文件定义结束
	// ///////////////////////////////////////////////////////////
	/**
	 * 刷新图像
	 */
	public void flush()
	{
		repaint();
		serviceRepaints();
	}
	/**
	 * 处理网络消息
	 * 
	 * @param type
	 * @param body
	 * @return
	 */
	// public abstract boolean recvMessage( int type, byte[] body );
}
